package zhcms.tool;

import java.lang.Math;

public class GyPage {
	private String outPut;
	private String fileName;
	private String pPublic = "p";
	private int pSize;
	private int pCur;
	private String pString;
	private int tPages;
	private int tRecord;

	public GyPage() {

	}

	public void setP(int pagesize, int total, String fileName, String pstring,
			int current) {
		StringBuffer tmpString = new StringBuffer();// 输出的临时数据
		this.tPages = (int) Math.ceil(total / pagesize) + 1;
		this.tRecord = total;
		
		if (current == 0) {
			// 如果当前页为空，当前页为1
			current = 1;
		}
		if (current > this.tPages) {
			current = this.tPages;
		}

		if (current < 1) {
			current = 1;
		}
		this.pCur = current; // 当前页
		this.pSize = pagesize; // 每页大小

		if (this.fileName == null) {
			// 当前的页面
			this.fileName = fileName;
		}

		if (this.pString == null) {
			// 所跟踪的参数
			this.pString = pstring;
		}

		if (this.tPages >= 1) {
			// 如果总页数大于等于1,即有数据

			tmpString.append("页次:<strong>" + this.pCur + "</strong>/<strong>"
					+ this.tPages + "</strong>&nbsp;每页:<strong>" + this.pSize
					+ "</strong>&nbsp;总记录数:<strong>" + this.tRecord
					+ "</strong>");

			if (current == 1) {
				tmpString
						.append("<font face=webdings color=red>9</font>&nbsp;");
			} else {
				tmpString.append("<a href=" + this.fileName + "?"
						+ this.pPublic + "=1" + "&" + (this.pString)
						+ " title='首页'><font face=webdings>9</font></a>&nbsp;");
			}
			if (current > 10) {
				tmpString
						.append("<a href="
								+ this.fileName
								+ "?"
								+ this.pPublic
								+ "="
								+ (this.pCur - 10)
								+ "&"
								+ (this.pString)
								+ " title='前十页'><font face=webdings>7</font></a>&nbsp;");
			}
			int BaseNum = ((int) (Math.floor((current - 1) / 10))) * 10;
			int start = BaseNum + 1;
			int end = BaseNum + 10;

			if (end > this.tPages) {
				end = this.tPages;
			}

			int i;

			for (i = start; i <= end; i++) {
				if (current == i) {
					tmpString.append("<font color=#FF0000><strong>" + i
							+ "</strong></font>&nbsp;");
				} else {
					tmpString.append("<a href=" + this.fileName + "?"
							+ this.pPublic + "=" + i + "&" + (this.pString)
							+ " title='第" + i + "页'><strong>" + i
							+ "</strong></a>&nbsp;");
				}
			}

			if ((BaseNum + 10) < this.tPages) {
				tmpString
						.append("<a href="
								+ this.fileName
								+ "?"
								+ this.pPublic
								+ "="
								+ (current + 10)
								+ "&"
								+ (this.pString)
								+ " title='下十页'><font face=webdings>8</font></a>&nbsp;");
			}
			if (current == this.tPages) {
				tmpString
						.append("<font face=webdings color=red>:</font>&nbsp;");
			} else {
				tmpString
						.append("<a href="
								+ this.fileName
								+ "?"
								+ this.pPublic
								+ "="
								+ (this.tPages)
								+ "&"
								+ (this.pString)
								+ " title='最后页'><font face=webdings>:</font></a>&nbsp;");
			}
			tmpString
					.append(" 转到:<input type=text name=PageId id=PageId size=4 maxlength=10  value="
							+ current
							+ " onkeydown=\"javascript:if(event.keyCode==13){location.href='"
							+ this.fileName
							+ "?"
							+ this.pPublic
							+ "='+PageId.value+'"
							+ "&"
							+ (this.pString)
							+ "'}\"><input type=button value=Go onClick=\"javascript:location.href='"
							+ this.fileName
							+ "?"
							+ this.pPublic
							+ "='+PageId.value+'"
							+ "&"
							+ (this.pString)
							+ "'\">");
		}

		String PageString = tmpString.toString();
		this.outPut = PageString;

	}

	public String outPut() {

		return this.outPut;
	}

	public String limit() {
		String tmpString = "";
		StringBuffer tpString = new StringBuffer();// 输出的临时数据
		tpString.append(" ");
		int cuur = (this.pCur - 1);
		if (cuur < 0)
			cuur = 1;
		tmpString = cuur * this.pSize + ", " + this.pSize;
		tpString.append(tmpString);
		String PageString = tpString.toString();
		return PageString;
	}
	
	public int getPageNum()
	{
		return this.tPages;
	}
}
